package cn.edu.sctu.sctu_lysd.presenter;

import android.content.Context;
import android.widget.Toast;

import cn.edu.sctu.sctu_lysd.bean.JsonBaseBeen;
import cn.edu.sctu.sctu_lysd.bean.JsonBaseListBean;
import cn.edu.sctu.sctu_lysd.bean.ParcelData;
import cn.edu.sctu.sctu_lysd.biz.MyAcceptBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.net.NetService;
import cn.edu.sctu.sctu_lysd.net.RetrofitHelper;
import cn.edu.sctu.sctu_lysd.utils.L;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by wangfuxin on 2017-04-17.
 */

public class MyAcceptPresenter implements MyAcceptBiz.Presenter {

    private Context mContext;
    private MyAcceptBiz.View mView;

    private NetService service;
    private SharePreferenceUtils sharePreferenceUtils;

    private Call<JsonBaseListBean<ParcelData>> parcelCall;

    public static final int ERROR_MISMATCH = 40000701;//扫码人与包裹是不匹配
    public static final int ERROR_REPEAT = 40000700;//该包裹已经完成，不必重复扫码
    public static final int ERROR_SUCCESS = 40001101;//包裹成功送达，系统账户转账失败

    public MyAcceptPresenter(Context mContext, MyAcceptBiz.View mView) {
        this.mContext = mContext;
        this.mView = mView;
    }

    @Override
    public void start() {

        RetrofitHelper helper = RetrofitHelper.getInstance();

        service = (NetService) helper.getService(NetService.class);

        sharePreferenceUtils = SharePreferenceUtils.getInstance(mContext);

    }

    @Override
    public void getOrder(long parcelId) {

        parcelCall = service.getOneParcel(parcelId);

        parcelCall.enqueue(new Callback<JsonBaseListBean<ParcelData>>() {
            @Override
            public void onResponse(Call<JsonBaseListBean<ParcelData>> call, Response<JsonBaseListBean<ParcelData>> response) {

                L.e("onResponse get One parcel", response.toString());

                if (response.body().getMeta().getCode() == 200) {

                    mView.getOrderSuccess(response.body().getData().get(0));
                }

            }

            @Override
            public void onFailure(Call<JsonBaseListBean<ParcelData>> call, Throwable t) {

                L.e("onResponse get One parcel erroe", t.getMessage());

                mView.getOrderFailed();

            }
        });

    }

    @Override
    public void finishParcel(long parcelId) {

       Call<JsonBaseBeen<Object>> call = service.finishParcel(sharePreferenceUtils.getString(CacheKeyConfig.CACHE_ACCESS_TOKEN),
                parcelId);


        call.enqueue(new Callback<JsonBaseBeen<Object>>() {
            @Override
            public void onResponse(Call<JsonBaseBeen<Object>> call, Response<JsonBaseBeen<Object>> response) {



                if (response.code() == 200){


                    switch (response.body().getMeta().getCode()) {


                        case ERROR_SUCCESS:

                            mView.finishParcelSuccess();

                            break;

                        case ERROR_REPEAT:

                            mView.finishParcelFailed(ERROR_REPEAT);
                            break;
                        case ERROR_MISMATCH:

                            mView.finishParcelFailed(ERROR_MISMATCH);

                            break;
                        case 200:
                            mView.finishParcelSuccess();
                            break;

                    }


                }
            }

            @Override
            public void onFailure(Call<JsonBaseBeen<Object>> call, Throwable t) {

                    mView.finishParcelFailed(-1);


            }
        });

    }
}
